import olproj from 'ol/proj'
import TileLayer from 'ol/layer/tile'
import TileGrid from 'ol/tilegrid/tilegrid'
import TileImage from 'ol/source/tileimage'
import projzh from 'projzh'
import olextent from 'ol/extent'
import Projection from 'ol/proj/projection'

/**
 * 百度地图坐标转换扩展
 */

const extent = [72.004, 0.8293, 137.8347, 55.8271]
// 定义百度坐标
// 地址：https://github.com/openlayers/openlayers/issues/3522
const baiduMercator = new Projection({
  code: 'baidu',
  extent: olextent.applyTransform(extent, projzh.ll2bmerc),
  units: 'm'
})
olproj.addProjection(baiduMercator)
olproj.addCoordinateTransforms('EPSG:4326', baiduMercator, projzh.ll2bmerc, projzh.bmerc2ll)
olproj.addCoordinateTransforms('EPSG:3857', baiduMercator, projzh.smerc2bmerc, projzh.bmerc2smerc)

/**
 * 百度地图来源
 * */
function baiduSource () {
  var resolutions = []
  for (var i = 0; i < 19; i++) {
    resolutions[i] = Math.pow(2, 18 - i)
  }
  var tilegrid = new TileGrid({
    origin: [0, 0],
    resolutions: resolutions,
    extent: olextent.applyTransform(extent, projzh.ll2bmerc)
  })
  var baiduSource = new TileImage({
    // projection: olproj.get('EPSG:3857'),
    projection: 'baidu',
    tileGrid: tilegrid,
    tileUrlFunction: function (tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ''
      }
      var z = tileCoord[0]
      var x = tileCoord[1]
      var y = tileCoord[2]
      if (x < 0) {
        x = 'M' + (-x)
      }
      if (y < 0) {
        y = 'M' + (-y)
      }
      return 'http://api0.map.bdimg.com/customimage/tile?qt=tile&x=' + x + '&y=' + y + '&z=' + z + '&styles=%3dt%3awater%7ce%3aall%7cc%3a%23566382%2ct%3aland%7ce%3aall%7cc%3a%23172137%2ct%3agreen%7ce%3aall%7cc%3a%23282f57%2ct%3amanmade%7ce%3aall%7cc%3a%233f4b8c%2ct%3abuilding%7ce%3aall%7cc%3a%233f4b8c%2ct%3aboundary%7ce%3ag%7cc%3a%23ffffffff%2ct%3arailway%7ce%3ag%7cc%3a%234f6b9e%2ct%3ahighway%7ce%3ag.s%7cv%3aon%7cc%3a%23202749%2ct%3aarterial%7ce%3ag.f%7cv%3aon%7cc%3a%234f6b9e%2ct%3alocal%7ce%3ag.f%7cc%3a%23303a6d%2ct%3alocal%7ce%3ag.s%7cv%3aon%7cc%3a%232d3667%2ct%3asubway%7ce%3aall%7cv%3aon%7cc%3a%23445195%2ct%3aall%7ce%3al.t.s%7cc%3a%23141831%2ct%3aall%7ce%3al.t.f%7cc%3a%235564b2%2ct%3apoi%7ce%3aall%7cv%3aoff%7cc%3a%23141831%2ct%3asubway%7ce%3aall%7cv%3aon%2ct%3aarterial%7ce%3ag.s%7cc%3a%23181e3e%2ct%3ahighway%7ce%3ag%7cc%3a%23324160%7cw%3a0.9%2ct%3ahighway%7ce%3al%7cv%3aon%7cc%3a%23172137%2ct%3alabel%7ce%3al.t.f%7cc%3a%23ffffffff%2ct%3aroad%7ce%3aall%7cv%3aoff%7cc%3a%23ffffffff'
    }
  })
  return baiduSource
}

const baiduLayer = new TileLayer({
  source: baiduSource()
})
baiduLayer.imgsrc = '/static/img/blue-map.png'
baiduLayer.clickFun = function (layers) {
  for (let layer of layers) {
    layer.setVisible(false)
  }
  baiduLayer.setVisible(true)
}
/* ===========百度地图来源结束============= */
/**
 * 百度地图来源
 * */
function baiduWSource () {
  var resolutions = []
  for (var i = 0; i < 19; i++) {
    resolutions[i] = Math.pow(2, 18 - i)
  }
  var tilegrid = new TileGrid({
    origin: [0, 0],
    resolutions: resolutions
  })
  var baiduSource = new TileImage({
    // projection: olproj.get('EPSG:3857'),
    projection: 'baidu',
    tileGrid: tilegrid,
    tileUrlFunction: function (tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ''
      }
      var z = tileCoord[0]
      var x = tileCoord[1]
      var y = tileCoord[2]
      if (x < 0) {
        x = 'M' + (-x)
      }
      if (y < 0) {
        y = 'M' + (-y)
      }
      return 'http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=' + x + '&y=' + y + '&z=' + z + '&styles=pl&udt=20151021&scaler=1&p=1'
    }
  })
  return baiduSource
}

const baiduWLayer = new TileLayer({
  source: baiduWSource()
})
// baiduWLayer.imgsrc = '/static/img/baidu-white-map.png'
baiduWLayer.clickFun = function (layers) {
  for (let layer of layers) {
    layer.setVisible(false)
  }
  baiduWLayer.setVisible(true)
}
/* ===========百度地图来源结束============= */

/**
 * 规划局服务
 */
function planSource () {
  // 获取坐标系参数
  var projection = olproj.get('EPSG:4326')
  // 分辨率
  var resolutions = [
    0.0027363801567048224,
    0.0017845957543727103,
    0.00118973050291514,
    7.138383017490841e-4,
    3.5691915087454206e-4,
    1.6656227040811962e-4,
    8.328113520405981e-5,
    5.9486525145757005e-5,
    4.1640567602029906e-5,
    2.3794610058302804e-5,
    1.7845957543727102e-5,
    1.1897305029151402e-5,
    8.32811352040598e-6,
    4.75892201166056e-6,
    2.37946100583028e-6
  ]
  // 贴图组
  var tilegrid = new TileGrid({
    origin: [-400, 400],
    resolutions: resolutions,
    tileSize: 256
  })
  // 地图服务来源
  var planSource = new TileImage({
    projection: projection,
    tileGrid: tilegrid,
    tileUrlFunction: function (tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ''
      }
      var z = tileCoord[0]
      var x = Math.abs(tileCoord[1])
      var y = Math.abs(tileCoord[2]) - 1

      return (
      // 'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/2015年卫星影像/MapServer/tile/' +
      // 'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/政务电子地图/MapServer/tile/'+
        'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/政务电子地图/MapServer/tile/' + z + '/' + y + '/' + x
      )
    }
  })
  return planSource
}
// 构建地图图层
var planLayer = new TileLayer({
  source: planSource()
})
planLayer.imgsrc = '/static/img/white-map.png'

// planLayer.imgsrc = '/static/img/map.svg'
planLayer.clickFun = function (layers) {
  for (let layer of layers) {
    layer.setVisible(false)
  }
  planLayer.setVisible(true)
}
/* ===============规划局底图================= */

/**
 * 规划局服务
 */
function rsSource () {
  // 获取坐标系参数
  var projection = olproj.get('EPSG:4326')
  // 分辨率
  var resolutions = [
    0.0027363801567048224,
    0.0017845957543727103,
    0.00118973050291514,
    7.138383017490841e-4,
    3.5691915087454206e-4,
    1.6656227040811962e-4,
    8.328113520405981e-5,
    5.9486525145757005e-5,
    4.1640567602029906e-5,
    2.3794610058302804e-5,
    1.7845957543727102e-5,
    1.1897305029151402e-5,
    8.32811352040598e-6,
    4.75892201166056e-6,
    2.37946100583028e-6
  ]
  // 贴图组
  var tilegrid = new TileGrid({
    origin: [-400, 400],
    resolutions: resolutions,
    tileSize: 256
  })
  // 地图服务来源
  var planSource = new TileImage({
    projection: projection,
    tileGrid: tilegrid,
    tileUrlFunction: function (tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ''
      }
      var z = tileCoord[0]
      var x = Math.abs(tileCoord[1])
      var y = Math.abs(tileCoord[2]) - 1

      return (
      // 'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/2015年卫星影像/MapServer/tile/' +
      // 'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/政务电子地图/MapServer/tile/'+
      //  'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/2015年卫星影像/MapServer/tile/' + z + '/' + y + '/' + x
      //  'http://10.32.208.34:8010/ServiceAdapter/MAP/%E6%94%BF%E5%8A%A1%E7%94%B5%E5%AD%90%E5%9C%B0%E5%9B%BE%EF%BC%88%E5%A4%9C%E5%85%89%E7%89%88%EF%BC%89/c5b4b22b812bf245ce3a3bccc850e904' + z + '/' + y + '/' + x
        'http://10.32.208.34:8010/ServiceAdapter/MAP/%E6%94%BF%E5%8A%A1%E7%94%B5%E5%AD%90%E5%9C%B0%E5%9B%BE%EF%BC%88%E5%A4%9C%E5%85%89%E7%89%88%EF%BC%89/c5b4b22b812bf245ce3a3bccc850e904/tile/' + z + '/' + y + '/' + x
      )
    }
  })
  return planSource
}
// 构建地图图层
var rsLayer = new TileLayer({
  source: rsSource()
})
rsLayer.imgsrc = '/static/img/RS-map.png'
rsLayer.clickFun = function (layers) {
  for (let layer of layers) {
    layer.setVisible(false)
  }
  rsLayer.setVisible(true)
}
//
//
//

// 首页地图
/**
 * 百度地图来源
 * */
// 创建首页底图
function indexMapBaiduSource () {
  var resolutions = []
  for (var i = 0; i < 20; i++) {
    resolutions[i] = Math.pow(2, 18 - i)
  }
  var tilegrid = new TileGrid({
    origin: [0, 0],
    resolutions: resolutions,
    extent: olextent.applyTransform(extent, projzh.ll2bmerc)
  })
  var baiduSource = new TileImage({
    // projection: olproj.get('EPSG:3857'),
    projection: 'baidu',
    tileGrid: tilegrid,
    tileUrlFunction: function (tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ''
      }
      var z = tileCoord[0]
      var x = tileCoord[1]
      var y = tileCoord[2]
      if (x < 0) {
        x = 'M' + (-x)
      }
      if (y < 0) {
        y = 'M' + (-y)
      }
      // t%3Abackground%7Ce%3Aall%7Cc%3A%2304061dff
      // t%3Abackground%7Ce%3Aall%7Cc%3A%2300ffffff%7Ch%3A%233d85c6
      // t%3Awater%7Ce%3Aall%7Cc%3A%233287cfff%2Ct%3Ahighway%7Ce%3Ag.f%7Cv%3Aoff%7Cc%3A%23000000%2Ct%3Ahighway%7Ce%3Ag.s%7Cv%3Aoff%7Cc%3A%23147a92%2Ct%3Aarterial%7Ce%3Ag.f%7Cv%3Aoff%7Cc%3A%23000000%2Ct%3Aarterial%7Ce%3Ag.s%7Cv%3Aoff%7Cc%3A%230b3d51%2Ct%3Alocal%7Ce%3Ag%7Cc%3A%23000000%2Ct%3Aland%7Ce%3Aall%7Cc%3A%2307114aff%2Ct%3Arailway%7Ce%3Ag.f%7Cv%3Aoff%7Cc%3A%23000000%2Ct%3Arailway%7Ce%3Ag.s%7Cv%3Aoff%7Cc%3A%2308304b%2Ct%3Asubway%7Ce%3Ag%7Cv%3Aoff%7Cl%3A-70%2Ct%3Abuilding%7Ce%3Ag.f%7Cc%3A%23000000%2Ct%3Aall%7Ce%3Al.t.f%7Cc%3A%23857f7f%2Ct%3Aall%7Ce%3Al.t.s%7Cc%3A%23000000%2Ct%3Abuilding%7Ce%3Ag%7Cc%3A%23022338%2Ct%3Agreen%7Ce%3Ag%7Cc%3A%23062032%2Ct%3Aboundary%7Ce%3Aall%7Cc%3A%2300ffffff%2Ct%3Amanmade%7Ce%3Ag%7Cc%3A%23022338%2Ct%3Apoi%7Ce%3Aall%7Cv%3Aoff%2Ct%3Aall%7Ce%3Al.i%7Cv%3Aoff%2Ct%3Aall%7Ce%3Al.t.f%7Cv%3Aon%7Cc%3A%232da0c6
      return 'http://api0.map.bdimg.com/customimage/tile?&scaler=1qt=tile&x=' + x + '&y=' + y + '&z=' + z + '&styles=t%3Awater%7Ce%3Aall%7Cc%3A%2304061dff%2Ct%3Ahighway%7Ce%3Ag.f%7Cv%3Aoff%7Cc%3A%23000000%2Ct%3Ahighway%7Ce%3Ag.s%7Cv%3Aoff%7Cc%3A%23147a92%2Ct%3Aarterial%7Ce%3Ag.f%7Cv%3Aoff%7Cc%3A%23000000%2Ct%3Aarterial%7Ce%3Ag.s%7Cv%3Aoff%7Cc%3A%230b3d51%2Ct%3Alocal%7Ce%3Ag%7Cc%3A%23000000%2Ct%3Aland%7Ce%3Aall%7Cc%3A%2307114aff%2Ct%3Arailway%7Ce%3Ag.f%7Cv%3Aoff%7Cc%3A%23000000%2Ct%3Arailway%7Ce%3Ag.s%7Cv%3Aoff%7Cc%3A%2308304b%2Ct%3Asubway%7Ce%3Ag%7Cv%3Aoff%7Cl%3A-70%2Ct%3Abuilding%7Ce%3Ag.f%7Cc%3A%23000000%2Ct%3Aall%7Ce%3Al.t.f%7Cc%3A%23857f7f%2Ct%3Aall%7Ce%3Al.t.s%7Cc%3A%23000000%2Ct%3Abuilding%7Ce%3Ag%7Cc%3A%23022338%2Ct%3Agreen%7Ce%3Ag%7Cc%3A%23062032%2Ct%3Aboundary%7Ce%3Aall%7Cc%3A%2300ffffff%2Ct%3Amanmade%7Ce%3Ag%7Cc%3A%23022338%2Ct%3Apoi%7Ce%3Aall%7Cv%3Aoff%2Ct%3Aall%7Ce%3Al.i%7Cv%3Aoff%2Ct%3Aall%7Ce%3Al.t.f%7Cv%3Aon%7Cc%3A%232da0c6'
    }
  })
  return baiduSource
}
// 输出首页地图
export const indexMapBaiduLayer = new TileLayer({
  source: indexMapBaiduSource()
})
// 输出规划局地图
export const planLayerMap = new TileLayer({
  source: planSource()
})
// 输出卫星地图
export const satelliteMapSource = new TileLayer({
  source: rsSource()
})

/* ===============新规划局地图================= */

// 百度地图（白色）初始状态隐藏
baiduWLayer.setVisible(false)
// 百度地图（黑色）初始状态隐藏
baiduLayer.setVisible(false)
// 规划局初始状态隐藏
planLayer.setVisible(true)
// 卫星遥感初始状态隐藏
rsLayer.setVisible(false)

export default [baiduLayer, baiduWLayer, planLayer, rsLayer]
